package windowFactoryPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class UserWindow extends WindowGenerator{
	//Declare window components
	private JLabel [] userLabelArray = new JLabel[7];
	static JLabel [] userDataArray = new JLabel[7];
	static String [] userData = {"__FirstName__ __LastName__", "__Street Number__ __Street Name__", 
		"__City__ __State__ __ZipCode__", "__Email__", "__Phone__", "__Last4OfCard__ __Exp Date__", "_UserSince_"};
	private JLabel jlblBlank, jlblBlank1, jlblBlank2;
	private JPanel centerPanel, eastPanel, userLabelPanel, userDataPanel;
	private JButton jbtViewDownloads, jbtViewReviews, jbtViewPurchases;
	Query q = new Query();

	public UserWindow(){
		//Set layout for parent JFrame
		setLayout(new BorderLayout());
		this.addWindowFocusListener(new WindowFocusListener(){
			@Override
			public void windowGainedFocus(WindowEvent e) {
				setLocationRelativeTo(null);				
			}
			@Override
			public void windowLostFocus(WindowEvent e) {
				setLocation(0,0);				
			}			
		});
		
		//Instantiate eastPanel
		eastPanel = new JPanel();
		eastPanel.setLayout(new GridLayout(6,0,10,10));
		eastPanel.setBorder(BorderFactory.createEtchedBorder());
		
		//Instantiate and add components to eastPanel -- Buttons and JLabels
		jlblBlank = new JLabel("");
		jlblBlank1 = new JLabel("");
		jlblBlank2 = new JLabel("");
		jbtViewDownloads = new JButton("View Downloads");
		jbtViewDownloads.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.userDownloadsQuery();
				WindowGenerator.generatePreviousDownloadsWindow();
			}
		});
		jbtViewPurchases = new JButton("View Purchases");
		jbtViewPurchases.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.userPurchasesQuery();
				WindowGenerator.generatePreviousPurchasesWindow();
			}
		});
		jbtViewReviews = new JButton("View Reviews");
		jbtViewReviews.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.userReviewsQuery();
				//WindowGenerator.generateUserReviewWindow();
			}
		});		
		eastPanel.add(jlblBlank);
		eastPanel.add(jlblBlank1);
		eastPanel.add(jlblBlank2);
		eastPanel.add(jbtViewDownloads);
		eastPanel.add(jbtViewPurchases);
		eastPanel.add(jbtViewReviews);
		
		//Add eastPanel to parent JFrame.EAST
		add(eastPanel, BorderLayout.EAST);
		
		//Instantiate centerPanel
		centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.setBorder(BorderFactory.createEtchedBorder());
		
		//Instantiate userLabelPanel
		userLabelPanel = new JPanel();
		userLabelPanel.setLayout(new GridLayout(7,0,10,10));
		
		//Populate JLabel array and add to userLabelPanel -- add Panel to centerPanel.WEST
		userLabelArray[0] = new JLabel("Name: ");
		userLabelArray[1] = new JLabel("Address: ");
		userLabelArray[2] = new JLabel("");
		userLabelArray[3] = new JLabel("Email: ");
		userLabelArray[4] = new JLabel("Phone: ");
		userLabelArray[5] = new JLabel("Card: ");
		userLabelArray[6] = new JLabel("User Since: ");
		for(int i = 0; i<userLabelArray.length; i++){
			if(i != 2) userLabelArray[i].setBorder(BorderFactory.createEtchedBorder());			
			userLabelPanel.add(userLabelArray[i]);
		}		
		centerPanel.add(userLabelPanel, BorderLayout.WEST);
		
		//Instantiate userDataPanel
		userDataPanel = new JPanel();
		userDataPanel.setLayout(new GridLayout(7,0,10,10));
		
		//Loop to populate userDataArray with data passed to String array from query -- add to panel
		for(int i = 0; i<userDataArray.length; i++){
			userDataArray[i] = new JLabel(userData[i]);
			userDataArray[i].setBorder(BorderFactory.createEtchedBorder());		
			userDataPanel.add(userDataArray[i]);
		}
		
		//add userDataPanel to centerPanel.CENTER
		centerPanel.add(userDataPanel, BorderLayout.CENTER);
		
		//add centerPanel to parent JFrame.CENTER
		add(centerPanel, BorderLayout.CENTER);
	}
}
